﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Flare.Pages.Hards
{

    public partial class HardMark : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Марки оборудования"; }
        }

        public HardMark()
        {
            InitializeComponent();
        }

        private void techType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MarkList.SelectedIndex == -1)
            {
                ((ObjectDataProvider)this.Resources["equipment_marksDataSet"]).MethodParameters[0] = (int?)techType.SelectedValue;
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            var data = new Flare.DB.Entity.EquipmentMark()
            {
                mark = techMark.Text,
                id_type = (int?)techType.SelectedValue,
                id_mark = (int?)MarkList.SelectedValue
            };

            try
            {
                new DB.equipment_marks().Edit(data);
                MarkList.SelectedItem = null;
                techMark.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_marksDataSet");
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MarkList.SelectedValue == null) {
                glob.kernel.ShowNotice("Значение [Марка оборудования] должно иметь значение");
                return;
            }

            int id_mark = (int)MarkList.SelectedValue;
            
            try
            {
                new DB.equipment_marks().Delete(id_mark);
                MarkList.SelectedItem = null;
                techMark.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_marksDataSet");
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            techType.SelectedIndex = -1;
            MarkList.SelectedIndex = -1;
            ((ObjectDataProvider)this.Resources["equipment_marksDataSet"]).MethodParameters[0] = null;
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new Flare.DB.Entity.EquipmentMark()
            {
                mark = techMark.Text,
                id_type = (int?)techType.SelectedValue
            };

            try
            {
                new DB.equipment_marks().Create(data);
                techMark.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("equipment_marksDataSet");
        }

        private void MarkList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MarkList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                techType.SelectedValue = ((Flare.DB.Entity.EquipmentMark)MarkList.SelectedItem).id_type;

                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

    }
}
